package dao

import javax.inject.Inject
import models.Tables._
import play.api.db.slick.{DatabaseConfigProvider, HasDatabaseConfigProvider}
import slick.jdbc.JdbcProfile

import scala.concurrent.{ExecutionContext, Future}

class BlockRangeDao @Inject()(protected val dbConfigProvider: DatabaseConfigProvider)
                             (implicit exec: ExecutionContext) extends
  HasDatabaseConfigProvider[JdbcProfile] {

  import profile.api._

  def insert(rows: Seq[BlockrangeRow]): Future[Unit] = {
    db.run(Blockrange ++= rows).map(_ => ())
  }

  def getAll: Future[Seq[BlockrangeRow]] = {
    db.run(Blockrange.result)
  }

  def getBySpecies(species1: String, species2: String): Future[Seq[BlockrangeRow]] = {
    db.run(Blockrange.filter(x => (x.querySpecies === species1 && x.refSpecies === species2) || (x.refSpecies === species1 && x.querySpecies === species2)).result)
  }

  def getById(id: Int): Future[BlockrangeRow] = {
    db.run(Blockrange.filter(_.id === id).result.head)
  }

  def getByPos(species: String, chr: String, start: Int, end: Int): Future[Seq[BlockrangeRow]] = {
    db.run(Blockrange.filter(x => (x.querySpecies === species && x.query === chr && x.queryStart <= start && x.queryEnd >= end) ||
      (x.refSpecies === species && x.ref === chr && x.refStart <= start && x.refEnd >= end)).result)
  }
}
